package com.oscar.ehome.view.messageview.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.oscar.ehome.R;
import com.oscar.ehome.bean.messageBean.MessageListBean;
import com.oscar.ehome.utils.basethem.TintManager;
import com.oscar.ehome.utils.system.SystemConfigFactory;
import com.oscar.ehome.utils.util.DateUtil;

import java.util.List;

/**
 * Created by oscar
 * author: MengWanhe
 * date: On 2017/7/4
 * 消息列表
 */

public class MessageListAdapter extends BaseAdapter {

    private List<MessageListBean> specailList;
    private LayoutInflater mInflater;
    private Context mContext = null;

    public MessageListAdapter(Context context, List<MessageListBean> specailList) {
        this.mContext = context;
        this.specailList = specailList;
        mInflater = LayoutInflater.from(context);
    }

    public void setLists(List<MessageListBean> specailList) {
        this.specailList = specailList;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return specailList.size();
    }

    @Override
    public Object getItem(int position) {
        return specailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.adapetr_messagelist, null);
            holder = new ViewHolder();
            holder.message_title = (TextView) convertView.findViewById(R.id.message_title);
            holder.message_time = (TextView) convertView.findViewById(R.id.message_time);
            holder.message_img = (ImageView) convertView.findViewById(R.id.message_img);
            holder.message_update = (ImageView) convertView.findViewById(R.id.message_update);
            holder.message_titles = (TextView) convertView.findViewById(R.id.message_titles);
            holder.message_stick = (TextView) convertView.findViewById(R.id.message_stick);
            holder.message_title_layout = (RelativeLayout) convertView.findViewById(R.id.message_title_layout);

            holder.message_title_layouts = (RelativeLayout) convertView.findViewById(R.id.message_title_layouts);
            holder.message_title_text = (TextView) convertView.findViewById(R.id.message_title_text);
            holder.message_time_text = (TextView) convertView.findViewById(R.id.message_time_text);
            convertView.setTag(holder); //
        } else {
            holder = (ViewHolder) convertView.getTag(); //
        }

        if (specailList.size() >= 0 || specailList != null) {
            MessageListBean listBean = specailList.get(position);
            String type = listBean.getType();
            String title = listBean.getTitle();
            String isSticky = listBean.getIsSticky();
            switch (type) {
                case "1"://通知
                    holder.message_img.setImageResource(R.mipmap.tongzhi1);
                    if ("1".equals(isSticky)) {
                        holder.message_titles.setText(R.string.message_inform);
                    } else {
                        holder.message_title_text.setText(R.string.message_inform);
                    }
                    break;
                case "2"://激励
                    holder.message_img.setImageResource(R.mipmap.jili1);
                    if ("1".equals(isSticky)) {
                        holder.message_titles.setText(R.string.message_exciting);
                    } else {
                        holder.message_title_text.setText(R.string.message_exciting);
                    }
                    break;
                case "3"://产品
                    holder.message_img.setImageResource(R.mipmap.chanpin1);
                    if ("1".equals(isSticky)) {
                        holder.message_titles.setText(R.string.message_product);
                    } else {
                        holder.message_title_text.setText(R.string.message_product);
                    }
                    break;
                case "4"://会议
                    holder.message_img.setImageResource(R.mipmap.huiyi1);
                    if ("1".equals(isSticky)) {
                        holder.message_titles.setText(R.string.message_meeting);
                    } else {
                        holder.message_title_text.setText(R.string.message_meeting);
                    }
                    break;
                case "5"://系统
                    holder.message_img.setImageResource(R.mipmap.xitong1);
                    if ("1".equals(isSticky)) {
                        holder.message_titles.setText(R.string.message_system);
                    } else {
                        holder.message_title_text.setText(R.string.message_system);
                    }
                    break;
            }

            String date = DateUtil.timeLogic(listBean.getUpdateDate());
            holder.message_title.setText(title);



            holder.message_stick.setTextColor(Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));
            TintManager.setTint(holder.message_stick, Color.parseColor(SystemConfigFactory.getInstance(mContext).getSystemConfig().getColor()));

            if ("1".equals(isSticky)) {//有置顶时
                holder.message_title_layout.setVisibility(View.VISIBLE);
                holder.message_title_layouts.setVisibility(View.GONE);
                holder.message_time.setText(date);
            } else {//无置顶时
                holder.message_title_layout.setVisibility(View.GONE);
                holder.message_title_layouts.setVisibility(View.VISIBLE);
                holder.message_time_text.setText(date);
            }
            if ("0".equals(listBean.getReadFlag())) {//未读时
                holder.message_update.setVisibility(View.VISIBLE);
            } else {
                holder.message_update.setVisibility(View.GONE);
            }

        }

        return convertView;
    }

    class ViewHolder {
        TextView message_time;
        TextView message_title;
        ImageView message_img;
        ImageView message_update;
        TextView message_titles;
        TextView message_stick;
        RelativeLayout message_title_layout;//有置顶的布局

        RelativeLayout message_title_layouts;//无置顶的布局
        TextView message_title_text;
        TextView message_time_text;
    }

}
